﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;


public partial class BasicBase_Vehicle_VehicleSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        //部门
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        //if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        //{
        //    DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        //    DepartmentList.DataBind();
        //    DepartmentListBind();
        //}
        //else
        //{
        //    DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
        //    DepartmentList.DataBind();
        //    DepartmentListBind();
        //}
        //车辆类型

        ddlType.DataTextField = "ItemName";
        ddlType.DataValueField = "ItemID";
        ddlType.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and ParentID='10' and ItemID<>'1099' order by SeqID,ItemID ");
        ddlType.DataBind();
        ddlClass.DataTextField = "ItemName";
        ddlClass.DataValueField = "ItemID";
        BindClass();
        ////车辆状态
        //State.DataTextField = "ItemName";
        //State.DataValueField = "ItemID";
        //State.DataSource = CodeItem.GetList2(" GroupID='XFCLZT' and ParentID='' order by SeqID,ItemID  ");
        //State.DataBind();
    }

    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        //DepartmentListBind();
    }

    //void DepartmentListBind()
    //{
    //    if (DepartmentList.SelectedValue == "")
    //    {
    //        UnitList.Items.Clear();
    //        UnitList.Items.Add(new ListItem("------------部门-----------------------", ""));
    //        UnitList.DataSource = null;
    //        UnitList.DataBind();
    //    }
    //    else
    //    {
    //        UnitList.Items.Clear();
    //        UnitList.Items.Add(new ListItem("------------部门-----------------------", ""));
    //        string strWhere = "(departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or departmentid='" + DepartmentList.SelectedValue + "'";
    //        UnitList.DataSource = Department.GetList(strWhere);
    //        UnitList.DataBind();
    //    }
    //}
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
    }

    private void BindClass()
    {

        if (ddlType.SelectedValue == "")
        {
            ddlClass.Items.Clear();
            ddlClass.Items.Add(new ListItem("------------类别-----------------------", ""));
            ddlClass.DataSource = "";
            ddlClass.DataBind();
        }
        else
        {
            ddlClass.Items.Clear();
            ddlClass.Items.Add(new ListItem("------------类别-----------------------", ""));
            ddlClass.DataSource = CodeItem.GetList2(" GroupID='XFZBLB' and  parentID='" + ddlType.SelectedValue + "' order by ItemID,SeqID ");
            ddlClass.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string where = "";
        if (txtVaule.Value != "")
        {
            string strDepartment = "";
            string[] departments = txtVaule.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < departments.Count(); i++)
            {
                strDepartment += "'" + departments[i] + "',";
            }

            strDepartment = " departmentid in (" + strDepartment.Substring(0, strDepartment.Length - 1) + ")";
            where += strDepartment;
        }
        else
        {
            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) != "1300")
            {
                where = " departmentid like '" + department.DepartmentID.Substring(0, 4) + "%' ";
            }
        }
        if (ddlType.SelectedValue != "")
        {
            if (ddlClass.SelectedValue == "")
            {
                where += where == "" ? "" : " And ";
                where += " VehicleClass like '" + ddlType.Text + "__' ";
            }
            else if (ddlClass.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                where += " VehicleClass='" + ddlClass.Text + "' ";
            }
        }
        where = getTextBoxString(where, "NumberPlate", NumberPlate);
        where = getTextBoxString(where, "VehicleName", VehicleName);
        where = getTextBoxString(where, "VehicleMode", VehicleMode);
        where = getTextBoxString(where, "EngineType", EngineType);
        where = getTextBoxString(where, "ChassisType", ChassisType);
        where = getCompareTextBoxString(where, "MaxDrinkDepth", MaxDrinkDepth, MaxDrinkDepthList);
        where = getCompareTextBoxString(where, "CapacityOfWater", CapacityOfWater, CapacityOfWaterList);
        where = getCompareTextBoxString(where, "CapacityOfFoam", CapacityOfFoam, CapacityOfFoamList);
        where = getCompareTextBoxString(where, "CapacityOfPowder", CapacityOfPowder, CapacityOfPowderList);
        where = getCompareTextBoxString(where, "TotalWeight", TotalWeight, TotalWeightList);
        where = getCompareTextBoxString(where, "Price", Price, PriceList);
        // where = getCompareTextBoxString(where, "FlowRate", FlowRate, FlowRateList);
        where = getCompareTextBoxString(where, "MaxPower", MaxPower, MaxPowerList);
        where = getCompareTextBoxString(where, "Passenger", Passenger, PassengerList);
        where = getCompareTextBoxString(where, "MaxSpeed", MaxSpeed, MaxSpeedList);
        where = getCompareTextBoxString(where, "MaxTorque", MaxTorque, MaxTorqueList);
        where = getCompareTextBoxString(where, "ConstantPressure", ConstantPressure, ConstantPressureList);
        where = getCompareTextBoxString(where, "Life", Life, LifeList);
        where = getCompareTextBoxString(where, "WorkHeight", WorkHeight, WorkHeightList);
        if (DateProducStart.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " DateProduc>='" + DateProducStart.Text + "' ";
        }
        if (DateProducEnd.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " DateProduc<'" + Convert.ToDateTime(DateProducEnd.Text).AddDays(1) + "' ";
        }
        if (DatePurchaseStart.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " DatePurchase>='" + DatePurchaseStart.Text + "' ";
        }
        if (DatePurchaseEnd.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " DatePurchase<'" + Convert.ToDateTime(DatePurchaseEnd.Text).AddDays(1) + "' ";
        }
        where = getTextBoxString(where, "Number", Number);
        where = getTextBoxString(where, "OtherParameters", OtherParameters);
        where = getTextBoxString(where, "Manufacturer", Manufacturer);
        where = getTextBoxString(where, "Address", Address);
        where = getTextBoxString(where, "Phone", Phone);
        where = getTextBoxString(where, "MostUse", MostUse);
        where = getTextBoxString(where, "UseMethod", UseMethod);
        where = getTextBoxString(where, "AlertInfo", AlertInfo);
        where = getCompareTextBoxString(where, "VehicleHeight", VehicleHeight, VehicleHeightList);
        where = getCompareTextBoxString(where, "VehicleWidth", VehicleWidth, VehicleWidthList);
        where = getCompareTextBoxString(where, "VehicleLength", VehicleLength, VehicleLengthList);
        where = getCompareTextBoxString(where, "PumpFlux", PumpFlux, PumpFluxList);
        //where = getCompareTextBoxString(where, "soldierCount", soldierCount, soldierCountList);
        where = getTextBoxString(where, "PumpType", PumpType);
        where = getTextBoxString(where, "RadioNO", RadioNO);
        if (Wlan.SelectedValue != "")
        {
            where += where == "" ? "" : " And ";
            where += " Wlan = " + Wlan.SelectedValue + " ";
        }
        //if (State.SelectedValue != "")
        //{
        //    where += where == "" ? "" : " And ";
        //    where += " State = '" + State.SelectedValue + "' ";
        //}
        //图片
        if (ImageList.SelectedValue == "1")
        {
            where += where.Length == 0 ? "" : " and ";
            where += " Picture <> '' ";
        }
        if (ImageList.SelectedValue == "0")
        {
            where += where.Length == 0 ? "" : " and ";
            where += " Picture = '' ";
        }
        Response.Redirect("VehicleList.aspx?where=" + Server.UrlEncode(where));
    }

    string getTextBoxString(string where, string DataFile, TextBox txt)
    {

        if (txt.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " " + DataFile + " like '%" + txt.Text + "%' ";
        }
        return where;
    }

    string getCompareTextBoxString(string where, string DataFile, TextBox txt, DropDownList ddl)
    {
        if (txt.Text != "")
        {
            where += where == "" ? "" : " And ";
            where += " " + DataFile + " " + ddl.SelectedValue + " " + txt.Text + " ";
        }

        return where;
    }

}
